﻿(function() {
  var __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  define(['jquery', 'use!underscore', 'use!backbone', 'album', 'text!templates/show_photo.html', 'text!templates/edit_photo.html', 'jquery_ui'], function($, _, Backbone, Album, ShowPhotoTemplate, EditPhotoTemplate) {
    var PhotoView;
    return PhotoView = (function(_super) {

      __extends(PhotoView, _super);

      function PhotoView() {
        return PhotoView.__super__.constructor.apply(this, arguments);
      }

      PhotoView.prototype.tagName = 'figure';

      PhotoView.prototype.attributes = {
        "class": 'photo'
      };

      PhotoView.prototype.initialize = function() {
        return this.model.on('error', (function(model, err) {
          console.log(err);
          return this.render('edit');
        }), this);
      };

      PhotoView.prototype.templateFunc = {
        show: _.template(ShowPhotoTemplate),
        edit: _.template(EditPhotoTemplate)
      };

      PhotoView.prototype.render = function(template) {
        this.$el.html(this.templateFunc[template](this.model.toJSON()));
        _.each($('.datepicker'), function(input) {
          return $(input).datepicker({
            dateFormat: 'yy-mm-dd',
            currentText: input.value
          });
        });
        return this;
      };

      PhotoView.prototype.events = {
        'click #edit': 'editModel',
        'click #cancel': 'cancelSave',
        'click #save': 'saveUpdate',
        'click #delete': 'deleteModel'
      };

      PhotoView.prototype.editModel = function(e) {
        e.preventDefault();
        return this.render('edit');
      };

      PhotoView.prototype.cancelSave = function(e) {
        e.preventDefault();
        return this.render('show');
      };

      PhotoView.prototype.saveUpdate = function() {
        var formData;
        formData = {};
        this.$el.find('input').each(function() {
          var $input;
          $input = $(this);
          return formData[$input.attr('id')] = $input.val();
        });
        console.log(formData);
        console.log(this.model);
        this.model.set(formData);
        return this.render('show');
      };

      PhotoView.prototype.deleteModel = function(e) {
        e.preventDefault();
        Album.url = '/Photo/Delete';
        this.remove();
        return this.model.destroy();
      };

      return PhotoView;

    })(Backbone.View);
  });

}).call(this);
